package top.noteday.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Date;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import entity.Page;
import entity.field.FieldDto;
import entity.field.FieldQuery;
import entity.result.EntityResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.noteday.dao.field.FieldDao;
import utils.common.StrUtil;
import entity.value.*;
import top.noteday.dao.value.ValueDao;
import top.noteday.provider.value.ValueService;

@Component
public class ValueServiceImpl implements ValueService {

	private Logger LOGGER = LoggerFactory.getLogger(ValueServiceImpl.class);

	@Autowired
	private ValueDao valueDao;
    @Autowired
    private FieldDao fieldDao;

    /*
    @Override
    PageResult<ValueDto> selectValue(ValueVo valueVo, Page page) {
        ValueQuery valueQuery = new ValueQuery();
        PageResult<ValueDto> result = new PageResult<>();

        // TODO 设置查询信息
        valueQuery.setCreator(userId);
        valueQuery.setPage(page);

        // 设置查询条数
        int count = valueDao.selectValueCount(valueQuery);
        result.setTotalCount(count);

        List<ValueDto> list = new ArrayList<>();
        if (count > 0) {
            list = valueDao.selectValue(valueQuery);

            // 返回值信息缺少时，赋值默认值
            for(CommissionDto dto : list) {
                // TODO 设置返回值默认信息
                //if(StrUtil.isEmpty(dto.getBorrowerPhone())) { dto.setBorrowerPhone(""); }
                //if(StrUtil.isEmpty(dto.getBuildArea())) { dto.setBuildArea(new BigDecimal("0.00")); }
            }
        }

        result.setData(list);
        result.setCode(ResponseCodeEnum.SUCCESS.getCode());
        result.setMess(ResponseCodeEnum.SUCCESS.getMessage());
        result.setSuccess(true);
        return result;
    }
    */

    /**
     * 查找数据
     **/
    @Override
    public Value selectById(Integer id) {
        ValueQuery query = new ValueQuery();
        query.setId(id);

        List<ValueDto> list = valueDao.selectByParam(query);

        return StrUtil.isEmpty(list) ? null : list.get(0);
    }

    /**
     * 保存数据
     */
    @Override
    public int saveValues(Value value) {
        if (StrUtil.isEmpty(value.getId())) {
            value.setCreator(0);
            value.setCreateTime(new Date());
            value.setOperator(0);
            value.setUpdateTime(new Date());
            valueDao.insert(value);
        }
        return 1;
    }

    /**
     * 获取详情
     */
    @Override
    public EntityResult<ValueDto> getFormValue(ValueVo vo) {
        LOGGER.info("获取详情{} = ", vo);
        EntityResult<ValueDto> result = new EntityResult<>();
        // Map<String, String> valueMap = new HashMap<>();

        Value value = this.selectById(vo.getId());
        ValueDto valueDto = new ValueDto();

        FieldQuery fieldQuery = new FieldQuery();
        fieldQuery.setFormId(value.getFormId());
        List<FieldDto> fieldDtoList = fieldDao.selectField(fieldQuery);

        JSONObject valueJSONObject = JSONObject.parseObject(JSONObject.toJSONString(value));
        // 模板对应关系
        Map<String, String> fieldMap = new HashMap();
        for (FieldDto dto : fieldDtoList) {
            Object obj = valueJSONObject.get(dto.getMapper());
            fieldMap.put(dto.getCode(), StrUtil.isEmpty(obj) ? "" : obj.toString());
        }
        valueDto.setFieldMap(fieldMap);
        valueDto.setFormId(value.getFormId());

        return result.responseSuccess(valueDto);
    }
}
